package interfaces;

import java.util.ArrayList;

import negocio.Categoria;

import com.example.money.R;

import databases.DsCategoria;
import databases.DsLancamento;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class CategoriasActivity extends Activity {
	
	private DsCategoria db;
	private DsLancamento dbLancamentos;
	
	ArrayList<Categoria> categoriasArrayList;
	ArrayAdapter<Categoria> categoriaAdapter;
	Categoria categoriaSelected;
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_categorias);
		
		db = new DsCategoria(this);
		dbLancamentos = new DsLancamento(this);
		
		db.open();
		dbLancamentos.open();
		
		montaListView();
		addListenersOnButtons();
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
	{
		super.onCreateContextMenu(menu, v, menuInfo);
		
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.renomear_excluir, menu);
		
		AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo;
		
		if(dbLancamentos.hasLancamentoCategoria(categoriaAdapter.getItem(info.position).getId()))
		{
			menu.getItem(1).setEnabled(false);
		}
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item)
	{
		AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
		categoriaSelected = categoriaAdapter.getItem(info.position);
		
		switch(item.getItemId())
		{
			case R.id.action_excluir:
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
		        
				builder.setMessage(R.string.categoria_excluir_confirm);
				builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
	                public void onClick(DialogInterface dialog, int id) 
	                {
	                	remove();
	                }
	            });
				builder.setNegativeButton(R.string.cancelar, null);
				
				builder.show();
				
				return true;
				
			case R.id.action_renomear:
				Intent intent = new Intent(CategoriasActivity.this, CategoriaActivity.class);
				intent.putExtra("id", categoriaSelected.getId());
				intent.putExtra("nome", categoriaSelected.getNome());
				startActivity(intent);
				
				return true;
				
			default:
				return super.onContextItemSelected(item);
		}
	}
	
	private void remove()
	{
		db.remove(categoriaSelected.getId());
		montaListView();
		Toast.makeText(this, R.string.categoria_excluir_sucesso, Toast.LENGTH_LONG).show();
	}
	
	private void montaListView(){
		categoriasArrayList = db.getAll();
		categoriaAdapter = new ArrayAdapter<Categoria>(this, android.R.layout.simple_list_item_1, categoriasArrayList);
		
		ListView listViewCategorias = (ListView)findViewById(R.id.listViewCategorias);
		registerForContextMenu(listViewCategorias);
		listViewCategorias.setAdapter(categoriaAdapter);
	}
	
	@Override
	public void onRestart()
	{
		super.onRestart();
		
		montaListView();
		categoriaAdapter.notifyDataSetChanged();
	}
	
	public void addListenersOnButtons() {
		Button buttonAdicionar = (Button)findViewById(R.id.button_adicionar);
		buttonAdicionar.setOnClickListener(new OnClickListener() {
 			@Override
			public void onClick(View arg0) {
				startActivity(new Intent(CategoriasActivity.this, CategoriaActivity.class));
			}
 
		});
		
		Button buttonVoltar = (Button)findViewById(R.id.button_voltar);
		buttonVoltar.setOnClickListener(new OnClickListener() {
 			@Override
			public void onClick(View arg0) {
				finish();
			}
 
		});
	}
}
